var util = require('/utils/util.js')
var msession = null;
var BaseUrl = util.baseUrl;
var token = util.token;
//app.js
App({
  onLaunch: function() {
    var that = this
    //获取设备信息
    wx.getSystemInfo({
      success: function(res) {
        console.log("获取设备信息")
        console.log("手机型号:" + res.model)
        console.log("客户端平台platform:" + res.platform)
        console.log("系统版本号system:" + res.system)


        that.globalData.deviceInfo = res;
      }
    });
    //从本地缓存中异步获取指定 key 对应的内容。
    wx.getStorage({ //将数据存储在本地缓存中指定的 key 中，会覆盖掉原来该 key 对应的内容，这是一个异步接口。
      key: 'userdata',
      success: function(res) {
        that.globalData.mohuanUser = res.data;
      }
    });
    that.getUserInfo();
  },

  getUserInfo: function() {
    var that = this
    //调用登录接口
    wx.login({
      success: function(mres) {
        console.log(mres)
        console.log("code信息" + mres);
        util.request("/c2mwebservice/api/v2/public/wxapp/3rdsession/" + mres.code, function (dataJson) {
            //dataJson.returnData.sessionKey
            console.log('==获取sessionKey==')
          console.log(dataJson)
            msession = dataJson.returnData.sessionKey;
        that.globalData.openId = dataJson.returnData.openId
            //获取用户信息
            wx.getUserInfo({
                success: function (res) {
                  // console.log('登录成功获取openid' )
                  // console.log( res)
                    that.globalData.userInfo = res.userInfo;
                    typeof cb == "function" && cb(that.globalData.userInfo);
                    that.getUsersInfo({ "content": res.encryptedData, "iv": res.iv, "sessionKey": msession });
                }
            })
        }, function (reason) {
        })
   
      }
    })

  },
  //获取用户信息
  getUsersInfo(mdata) {
    var that = this;
    //请求首页数据
    let app = getApp()
    wx.request({
      url: BaseUrl + "/c2mwebservice/api/v2/public/wxapp/owndata",
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
        'Content-Encoding': 'gzip',
        'X-AUTH-TOKEN': token
      },
      data: mdata,
      success: function(res) {
        console.log('解密的数据')
        console.log(res)
        that.globalData.userInfo = res.data.returnData;
        console.log("输出解密成功" + that.globalData.userInfo);
      },
      fail: function(res) {}
    })
  },
  onShareAppMessage: function() {
    return {
      title: 'Cotte',
      desc: '酷特智能'
    }
  },
  globalData: {
    openId: '',
    userInfo: null,
    imageRequestId: null,
    mohuanUser: null,
    encrptdatadata: {
      userid: null,
      usertoken: null,
    },
    receiveInfo: null,
    deviceInfo: null
  },

})